import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_widget.dart';
import 'package:pornhub_app/components/no_more/no_data_scroll_view.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/model/mine/publications_videos_model.dart';
import 'package:pornhub_app/model/video_base_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/mine/my_publications/controllers/dy_page_controller.dart';
import 'package:pornhub_app/views/douyin/short_video_list_cell.dart';

import 'item_dy_view.dart';

class DyPage extends StatefulWidget {
  const DyPage({super.key});

  @override
  State<StatefulWidget> createState() => _DyPageState();
}

class _DyPageState extends State<DyPage> with SingleTickerProviderStateMixin {
  final controller = Get.find<DyPageController>();

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      child: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return [
            SliverAppBar(
              pinned: true,
              title: TabBar(
                controller: controller.tabController,
                isScrollable: false,
                labelColor: COLOR.color_D8201D,
                unselectedLabelColor: COLOR.color_333333,
                indicator: const BoxDecoration(),
                dividerHeight: 0,
                labelStyle:
                    TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w600),
                tabs: controller.tabs.map((entry) {
                  return Container(
                    height: 23.h,
                    padding: EdgeInsets.symmetric(horizontal: 14.w),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(11.5.w),
                    ),
                    alignment: Alignment.center,
                    child: TextView(
                      text: entry.text ?? "",
                      // color: COLOR.white,
                    ),
                  );
                }).toList(),
              ),
              elevation: 0,
              leadingWidth: 0,
              backgroundColor: Colors.white,
              shadowColor: const Color.fromRGBO(0, 0, 0, 0),
              surfaceTintColor: Colors.white,
              leading: const SizedBox.shrink(),
            ),
          ];
        },
        body: TabBarView(
          controller: controller.tabController,
          children: [
            Obx(
              () => _buildTabContent(controller.publications.value, 1),
            ),
            Obx(
              () => _buildTabContent(controller.underReviews.value, 2),
            ),
            Obx(
              () => _buildTabContent(controller.rejecteds.value, 3),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTabContent(List<VideoBaseModel> videos, int fromType) {
    return BaseRefreshWidget(
      controller,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 14.w),
        child: videos.isNotEmpty
            ? MasonryGridView.count(
                crossAxisCount: 2,
                itemCount: videos.length,
                mainAxisSpacing: 8.w,
                crossAxisSpacing: 4.w,
                itemBuilder: (context, index) {
                  final video = videos[index];
                  final h = video.height! == 0
                      ? 273.w
                      : video.height! / video.width! * 173.w;
                  return ShortVideoListCell(
                    width: 183.w,
                    imageHeight: h,
                    model: video,
                  ).onTap(
                    () {
                      Get.toShortVideoPlay(videos, idx: index);
                    },
                  );
                },
              ).marginOnly(left: 14.w, right: 14.w, top: 9.w)
            : const NoDataScrollView(),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
